<template>
  <BasicModal v-bind="$attrs" @register="registerModal" width="500px" :title="title" @ok="handleSubmit" destroyOnClose>
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>

<script lang="ts" setup>
  import { BasicModal, useModalInner } from "/@/components/Modal"
  import { BasicForm, useForm } from "/@/components/Form"
  import { formSchema } from "../data"
  import { ref, unref } from "vue"
  import * as userApi from "/@/api/iam/user"
  import { useUserStore } from "/@/store/modules/user"
  import { useMessage } from "/@/hooks/web/useMessage"

  const userStore = useUserStore()
  const { createMessage } = useMessage()
  const [registerForm, { resetFields, setFieldsValue, validate, updateSchema }] = useForm({
    schemas: formSchema,
    labelWidth: 70,
    wrapperCol: { lg: 24, md: 24 },
    showActionButtonGroup: false,
  })
  const userDetail = ref<any>({})
  const isUpdate = ref<boolean>(false)
  const title = ref<string>("")
  const emit = defineEmits(["register", "success"])
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    await resetFields()
    setModalProps({ confirmLoading: false })
    title.value = "编辑个人资料"
    if (data.record.post) {
      data.record.post = data.record.post.split(",")
    }
    if (data.record.relTenantIds) {
      data.record.relTenantIds = data.record.relTenantIds.split(",")
    }
    userDetail.value = data.record
    if (data.record.birthday === "未填写") {
      data.record.birthday = undefined
    }
    await setFieldsValue({ ...data.record })
  })

  /**
   * 数据修改和新增
   */
  async function handleSubmit() {
    try {
      let values = await validate()
      setModalProps({ confirmLoading: true })
      //提交表单
      await userApi.updateMeInfo(values)
      createMessage.success("更新成功")

      //更新缓存
      Object.assign(userDetail.value, values)
      userStore.setUserInfo(unref(userDetail))
      emit("success")
      closeModal()
    } finally {
      setModalProps({ confirmLoading: false })
    }
  }
</script>
